package com.app.miwawa.api.system.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.time.DateUtils;
import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.entity.CommonEntityUUID;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.ext.util.DateUtil.DatePattern;
import org.framework.common.ext.util.StringUtil;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 话时购买表
 * <p>描述: 系统管理</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年9月19日 下午12:53:52
 * @version 1.0.2016
 */
@Entity @Table(name = "TB_SYSTEM_CALLERS")
public class Callers extends CommonEntityUUID {

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = -3069832932697309333L;

	// 机构
	@Column(name = "dbname", length = 10, nullable = true)
	private String dbname;
	
	// 单价(元/分钟)
	@Column(name = "unit", nullable = true)
	private Float unit;
	
	// 总价(元“课时*60*单价”)
	@Column(name = "total", nullable = true)
	private Float total;
	
	// 话时(小时)
	@Column(name = "length", nullable = true)
	private Long length;
	
	// 已耗(分钟)
	@Column(name = "useing", nullable = true)
	private Float useing;
	
	// 创建时间
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	@Column(name = "create_time", nullable = true)
	private Date createTime;
	
	// 更新时间
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	@Column(name = "update_time", nullable = true)
	private Date updateTime;
	
	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#save(java.lang.String[])
	 * @author chanlong(陈龙)
	 * @date 2016年9月20日 下午12:33:28
	 */
	@Override
	public Object save(final String... options) throws Exception {
		// 获取参数
		int sec = Integer.valueOf(options[0]);
		Date time = DateUtils.parseDate(options[1], DatePattern.DATETIME.value());
		
		// 设置参数
		this.useing += StringUtil.round(sec, 60, 2);
		this.updateTime = time;
		
		// 保存实体
		return ProcessModel.factory().bo.update(this);
	}

	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#loader(java.lang.String[])
	 * @author chanlong(陈龙)
	 * @date 2016年9月20日 下午12:16:14
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Callers loader(final String... options) throws Exception {
		return (Callers)ProcessModel.factory().bo.loader((Class<CommonEntityAware<String>>)this.getClass(), "dbname", dbname);
	}

	public Callers() {
		this.createTime = new Date();
		this.status = SystemConstants.DICT_STATUS_DEFAULT;
		this.invalid = SystemConstants.DICT_DELETE_INVALID;
	}
	
	public Callers(String dbname){
		this.dbname = dbname;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public Float getUnit() {
		return unit;
	}

	public void setUnit(Float unit) {
		this.unit = unit;
	}

	public Float getTotal() {
		return total;
	}

	public void setTotal(Float total) {
		this.total = total;
	}

	public Long getLength() {
		return length;
	}

	public void setLength(Long length) {
		this.length = length;
	}

	public Float getUseing() {
		return useing;
	}

	public void setUseing(Float useing) {
		this.useing = useing;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
}
